package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.DishDTO;
import com.tm.entity.CategoryEntity;
import com.tm.entity.DishEntity;
import com.tm.service.DishService;
import com.tm.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @PROJECT_NAME: tm-wm-syb
 * @DESCRIPTION:
 * @USER: DELL
 * @DATE: 2023/4/5 10:25
 */
@Api(value = "菜品管理")
@RestController
@RequestMapping("dish")
public class DishConttroller {

    @Resource
    private DishService dishService;

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param dishDTO
     * @return
     */
    @ApiOperation("查询分类")
    @RequestMapping("page")
    public R page(@RequestParam(required = false, defaultValue = "1") Integer page,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize, DishDTO dishDTO) {
        Page<DishEntity> dishEntityPage = dishService.pageList(page, pageSize, dishDTO);
        return R.success(dishEntityPage);

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除分类")
    @DeleteMapping("deleteDish"+"{ids}")
    public R deleteDish(@PathVariable("ids") List<Long> ids) {
        dishService.removeByIds(ids);
        return R.success();
    }

    /**
     * 查询分类
     * @return
     */
    @ApiOperation("查询id")
    @GetMapping("/{id}")
    public R queryDishById(@PathVariable Long id){
        DishEntity byId = dishService.getById(id);
        return R.success(byId);
    }

    /**
     * 新增菜品
     * @param dishEntity
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public R adddish(@RequestBody DishEntity dishEntity){
      Boolean fals  = dishService.adddish(dishEntity);
        return R.success(fals);
    }

    /**
     * 修改
     * @param dishEntity
     * @return
     */
    @ApiOperation("修改分类")
    @PutMapping
    public R editDish(@RequestBody DishEntity dishEntity){
       UpdateWrapper<DishEntity> updateWrapper =new UpdateWrapper<>();
       updateWrapper.eq("id",dishEntity.getId());
        boolean update = dishService.update(dishEntity, updateWrapper);
        return R.success(update);
    }
    @GetMapping("list")
    public R queryDishlist(){
        List<DishEntity> list = dishService.list();
        return R.success(list);
    }

    /**
     * 状态
     * @param ids
     * @return
     */
    @PostMapping("zhuangtai")
    public R zhuangtai(String ids){
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            if(split[i]!=null && split[i]!=""){
                List<Long> collect = Arrays.stream(split).map(e -> Long.parseLong(e)).collect(Collectors.toList());
                return dishService.plqs(collect);
            }
        }
        return R.success();
    }




}
